package com.wzy.mall.utils;

import org.apache.ibatis.io.Resources;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class HTTPUtils {

    // 获取请求体中的JSON数据
    public static String getJSONRequest(HttpServletRequest request) throws IOException {
        ServletInputStream is = request.getInputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((length = is.read(bytes)) != -1) {
            bos.write(bytes);
        }
        String requestBody = bos.toString("utf8");
        return requestBody;
    }

    /**
     *  获取Image图片的路径前缀
     * @return  返回图片URL前缀
     * @throws IOException  抛出
     */
    public static String getImageBaseURL() throws IOException {
        // 读取配置文件中的imgURL
        Properties properties = new Properties();
        InputStream is = Resources.getResourceAsStream("config.properties");
        properties.load(is);
        return properties.getProperty("imageBaseURL");
    }
}
